using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml;

namespace Omniture.API
{
	public class PageItem
	{
		#region Class Members

		private readonly DateTime PivotDate = new DateTime(2000, 1, 1);

		private TrackReferenceCollection _propList = null;
		private TrackReferenceCollection _evarList = null;
		private TrackReferenceCollection _eventList = null;
		private string _pageName = "";
		private string _channelName = "";
		private string _formName = "";
		private string _pageURL = "";
		private int _id = 0;
		private string _description = "";
		private OmnitureProject _parent = null;

		#endregion

		#region Constructor

		public PageItem(OmnitureProject parent)
		{
			_parent = parent;

			_propList = new TrackReferenceCollection(this, TrackItemTypeConstants.PropItem);
			_evarList = new TrackReferenceCollection(this, TrackItemTypeConstants.EvarItem);
			_eventList = new TrackReferenceCollection(this, TrackItemTypeConstants.EventItem);
		}

		#endregion

		#region Events

		#endregion

		#region Property Implementations

		[Browsable(false)]
		public OmnitureProject Parent
		{
			get { return _parent; }
		}

		[Browsable(true)]
		[Description("A description for this object.")]
		[Category("General")]
		public string Description
		{
			get { return _description; }
			set { _description = value; }
		}

		[Browsable(false)]
		public int Id
		{
			get { return _id; }
			internal set { _id = value; }
		}

		[Browsable(true)]
		[Description("Determines the unique URL for the underlying page")]
		[Category("General")]
		public string PageURL
		{
			get { return _pageURL; }
			set { _pageURL = value; }
		}

		[Browsable(true)]
		[Description("Determines the friendly name of this page")]
		[Category("General")]
		public string PageName
		{
			get { return _pageName; }
			set { _pageName = value; }
		}

		[Browsable(true)]
		[Description("Determines the channel of this page")]
		[Category("General")]
		public string ChannelName
		{
			get { return _channelName; }
			set { _channelName = value; }
		}

		[Browsable(true)]
		[Description("Determines the name of the ASP.NET form at runtime")]
		[Category("General")]
		public string FormName
		{
			get { return _formName; }
			set
			{
				if (TrackItemBase.ValidateName(value))
					throw new Exception("The form name is not a valid token.");
				_formName = value;
			}
		}		

		[Browsable(false)]
		[Description("The page Prop list")]
		[Category("Data")]
		public TrackReferenceCollection PropList
		{
			get { return _propList; }
		}

		[Browsable(false)]
		[Description("The page Evar list")]
		[Category("Data")]
		public TrackReferenceCollection EVarList
		{
			get { return _evarList; }
		}

		[Browsable(false)]
		[Description("The page Event list")]
		[Category("Data")]
		public TrackReferenceCollection EventList
		{
			get { return _eventList; }
		}

		#endregion

		#region Methods

		public override string ToString()
		{
			return this.PageName;
		}

		public void Clean(ITrackItem item)
		{
			this.EVarList.Clean(item);
			this.PropList.Clean(item);			
			this.EventList.Clean(item);
		}

		#endregion

		#region IXmlable Members

		public virtual void LoadXml(XmlNode parentNode)
		{
			_channelName = XmlHelper.GetAttribute(parentNode, "ChannelName", _channelName);
			_formName = XmlHelper.GetAttribute(parentNode, "FormName", _formName);
			_pageName = XmlHelper.GetAttribute(parentNode, "Name", _pageName);
			_pageName = XmlHelper.GetAttribute(parentNode, "PageName", _pageName);
			_pageURL = XmlHelper.GetAttribute(parentNode, "PageURL", _pageURL);
			_description = XmlHelper.GetAttribute(parentNode, "Description", _description);

			XmlNode evarNode = parentNode.SelectSingleNode("EVarList");
			this.EVarList.LoadXml(evarNode);

			XmlNode eventNode = parentNode.SelectSingleNode("EventList");
			this.EventList.LoadXml(eventNode);

			XmlNode propNode = parentNode.SelectSingleNode("PropList");
			this.PropList.LoadXml(propNode);

		}

		public virtual void SaveXml(XmlNode parentNode)
		{
			XmlHelper.AddAttribute(parentNode, "ChannelName", this.ChannelName);
			XmlHelper.AddAttribute(parentNode, "FormName", this.FormName);
			XmlHelper.AddAttribute(parentNode, "PageName", this.PageName);
			XmlHelper.AddAttribute(parentNode, "PageURL", this.PageURL);
			XmlHelper.AddAttribute(parentNode, "Description", this.Description);

			//Save the collections
			this.EVarList.SaveXml(XmlHelper.AddNode(parentNode, "EVarList"));
			this.EventList.SaveXml(XmlHelper.AddNode(parentNode, "EventList"));
			this.PropList.SaveXml(XmlHelper.AddNode(parentNode, "PropList"));

		}

		#endregion

	}
}